package com.xukang.qd.client;

import com.xukang.qd.util.JacksonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/send")
public class SendController {

    @Autowired
    private JmsTemplate jmsTemplate;
    @Resource
    private  ProducerIbmMqUtil producerIbmMqUtil;
    @Resource
    private ConsumerIbmMqUtil consumerIbmMqUtil;

    public static Logger logger= LoggerFactory.getLogger(SendController.class);

    /**
     * 调用jmsTemplate 发送消息到队列
     * @param m
     */
    @RequestMapping("/")
    public void sendMessage(final String m){
        // 发送信息到队列
        jmsTemplate.setReceiveTimeout(3000);
        jmsTemplate.send(new MessageCreator() {
            @Override
            public javax.jms.Message createMessage(javax.jms.Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText(m);
                textMessage.setJMSCorrelationID("xukang123");

                textMessage.setJMSType("1");
                return textMessage;
            }
        });
        // 接收信息从队列
    }
    /**
     * 通过调用sendAndReceive方法，实现发送消息之后可以同步接收返回信息。
     * @param m
     */
//    @RequestMapping("/sar")
//    public void sendAndReceive(final String m){
//        jmsTemplate.setReceiveTimeout(3000);
//        Message replyMsg = jmsTemplate.sendAndReceive(new MessageCreator() {
//            @Override
//            public Message createMessage(Session session) throws JMSException {
//                TextMessage textMessage = session.createTextMessage();
//                textMessage.setText(m);
//                textMessage.setJMSCorrelationID("xukang123");
//
//               // textMessage.setJMSType("1");
//                return textMessage;
//            }
//        });
//        logger.info(replyMsg.toString());
//    }
    /**
     * 根据MQQueueManager 创建链接,模拟前端请求
     * @param str
     */
    @RequestMapping("/demo")
    @ResponseBody
    public String sendMessage2(String str, HttpServletResponse response){
        logger.info("发送请求");
        Map<String, Object> maps = new HashMap<String, Object>();
        //作为请求端标志，固定写法
        maps.put("key", "request");
        // 作为请求端身份标志，例如：cgs，fpzd
        maps.put("appcode", "dzswj");
        //作为请求代号
        maps.put("method","T1010");
        //作为请求内容,自主封装
        maps.put("data", str);
        logger.info("发送请求内容：{}",JacksonUtils.mapToJson(maps));
        String message = producerIbmMqUtil.sendRequest( JacksonUtils.mapToJson(maps));
        String result = consumerIbmMqUtil.retriResp(message);
        logger.info("请求响应结果:{}",result);
        return result;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        ProducerIbmMqUtil producerIbmMqUtil = new ProducerIbmMqUtil();
        producerIbmMqUtil.setQueueName("queu1");
        producerIbmMqUtil.setQueueManagerName("mq2");
        producerIbmMqUtil.setChannel("m2");
        producerIbmMqUtil.setPort(1414);
        producerIbmMqUtil.setCCSID(1381);
        producerIbmMqUtil.setHostname("127.0.0.1");
        System.out.println("第一步");
        String message = producerIbmMqUtil.sendRequest("xukang3333333333");
        //  String message = "414d51206d7132202020202020202020f0404a5d2017b102";
        ConsumerIbmMqUtil consumerIbmMqUtil = new ConsumerIbmMqUtil();
        consumerIbmMqUtil.setQueueManagerName("mq2");
        consumerIbmMqUtil.setQueueName("queu1");
        System.out.println("第二步");
        consumerIbmMqUtil.retriRequest( message);
        producerIbmMqUtil.setQueueName("queu2");
        System.out.println("第三步");
        producerIbmMqUtil.sendResp("xukang2222222222ccvflla3",message);
        consumerIbmMqUtil.setQueueName("queu2");
        System.out.println("第四步");
        consumerIbmMqUtil.retriResp( message);
    }
}
